package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("adminCategoryController")
@Slf4j
@Api(tags = "分类相关接口")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
    * 分类分页查询
    * @param categoryPageQueryDTO
    * @return
    * */
    @GetMapping("/page")
    @ApiOperation("分页查询分类")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询分类");
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
    * 新增分类
    * @param categoryDTO
    * @return
    * */
    @ApiOperation("新增分类")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success("添加成功");
    }

    /**
    * 启用禁用分类
    * @param status
    * @param id
    * @return
    * */
    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result StartOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用分类：{}",id);
        categoryService.startOrStop(status,id);
        return Result.success("操作成功");
    }


    /**
    * 修改分类
    * @param categoryDTO
    * @return
    * */
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类：{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success("修改成功");
    }


    /**
    * 删除分类
    * @param id
    * @return
    * */
    @ApiOperation("删除分类")
    @DeleteMapping
    public Result delete(Long id) {
        log.info("删除分类：{}",id);
        categoryService.deleteById(id);
        return Result.success("删除成功");
    }


    /**
    * 根据类型查询
    * @param type
    * @return
    * */
    @ApiOperation("根据类型查询")
    @GetMapping("/list")
    public Result<List> list(Integer type) {
        log.info("根据类型查询：{}",type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }



}
